package com.cunyu.util;

import cn.tom.db.jdbc.simple.DBUtil;
import cn.tom.httpclient.HttpRequest;
import cn.tom.httpclient.HttpRequest.Method;
import cn.tom.httpclient.util.LogUtil;
import cn.tom.httpclient.util.OKHttpUtil;
import cn.tom.kit.StringUtil;
import cn.tom.kit.ThreadPool;
import org.apache.commons.codec.binary.Base64;
/**
 * 	推送 消息分三块
	1. 电池信息, 充电, 电量
	2. 震动报警

 Base64.encodeBase64String(encrypted);
 byte[] encrypted1 =Base64.decodeBase64(text);
 * 	@author tomsun	
 */
public class JPushUtil {
	
	public static String APPKEY = "17e3f404eea7a44ac08ddb67";
    public static String MASTERSECRET = "7f5102b5d7d7ed2caafe2681";
	
	/**
	 * 推送所有设备
	 * @param alert
	 * @return
	 * @throws Exception
	 */
	public static String pushAll(String alert) throws Exception {
		HttpRequest request = new HttpRequest("https://api.jpush.cn/v3/push", Method.post);
		request.setHeader("Authorization", "Basic " + Base64.encodeBase64String((APPKEY+":"+MASTERSECRET).getBytes()));
		request.setHeader("Content-Type", "application/json");
		//'options':{'apns_production':false},  //TODO 去掉修改正式环境, 或改true
		byte content[] = ("{'platform':'all','audience':'all','notification':{'alert':'" + alert + "'}}").replace('\'', '"').getBytes("UTF-8");
		request.setRequestBody(content);
		LogUtil.info(request.getUrl()+"::"+new String(content));
		return OKHttpUtil.request(request).getResponseString();
	}
	
	/**
	 * 推送指定注册设备
	 * @param alert
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public static String pushByid(String alert,String sound, String[] id ) throws Exception{
		HttpRequest request = new HttpRequest("https://api.jpush.cn/v3/push", Method.post);
		request.setHeader("Authorization", "Basic " + Base64.encodeBase64String((APPKEY+":"+MASTERSECRET).getBytes()));
		request.setHeader("Content-Type", "application/json");
		String ids = StringUtil.join(id, ",");
		sound = StringUtil.isEmpty(sound) ? "default" : sound;
		//'options':{'apns_production':false}, 'ios' : {'sound': 'default'}  //TODO 去掉修改正式环境, 或改true IOS需要加默认推送音 
		//TODO 正式环境 'apns_production':true
        String pcontent ="{'options':{'apns_production':false},'platform':'all','audience':{'alias':["+ids+"]},"
				//+ "'message':{'msg_content':'"+alert+"','extras': {'sound': '"+sound+"'}}," //自定义消息
				+ "'notification':{'alert':'"+alert+"','ios':{'sound': 'default'}}}"; //通知,IOS 专用
		byte content [] = pcontent.replace('\'', '"').getBytes("UTF-8");
		request.setRequestBody(content);
		LogUtil.info(request.getUrl()+"::"+new String(content));
		return OKHttpUtil.request(request).getResponseString();
	}

	
	
	/**
	 */
	public static void pushMsg(final int uid, String title, final String content){
		ThreadPool.exec(new Runnable() {
			@Override
			public void run() {
				try{
					pushByid(content, "default", new String[]{ "'"+uid+"'"});
				}catch(Exception e){
					LogUtil.error("pushMsg", e);
				}
			}
		});
	}
	
	
	/**
	 * 运维端推送
	 * 只供运维使用
	 */
	static final String YW_APPKEY ="89b00fa7bd5f2ae5eb1d80e9";
	static final String YW_MASTERSECRET ="fe51100be03dee9a1144797c";
	
	public static String pushYwByid(String alert,String sound, String[] id ) throws Exception{
		HttpRequest request = new HttpRequest("https://api.jpush.cn/v3/push", Method.post);
		request.setHeader("Authorization", "Basic " + Base64.encodeBase64String((YW_APPKEY+":"+YW_MASTERSECRET).getBytes()));
		request.setHeader("Content-Type", "application/json");
		String ids = StringUtil.join(id, ",");
		sound = StringUtil.isEmpty(sound) ? "default" : sound;
		//'options':{'apns_production':false}, 'ios' : {'sound': 'default'}  //TODO 去掉修改正式环境, 或改true IOS需要加默认推送音 
		String pcontent ="{'options':{'apns_production':false},'platform':'all','audience':{'alias':["+ids+"]},"
						//+ "'message':{'msg_content':'"+alert+"','extras': {'sound': '"+sound+"'}}," //自定义消息
						+ "'notification':{'alert':'"+alert+"'}}"; //通知,IOS 专用
		byte content [] = pcontent.replace('\'', '"').getBytes("UTF-8");
		request.setRequestBody(content);
		LogUtil.info(request.getUrl()+"::"+new String(content));
		return OKHttpUtil.request(request).getResponseString();
	}

	
	public static void main(String[] args) throws Exception {
		// 62   69
		String str = pushByid("后台测试 推送IOS", "default",  new String[]{"'89'"});
		System.out.println(str);
	}

	
}
